package com.wz.project.system.prjgroup.controller;

import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.PageDomain;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.framework.web.page.TableSupport;
import com.wz.project.system.employee.domain.Employee;
import com.wz.project.system.employee.domain.EmployeeVO;
import com.wz.project.system.employee.mapper.EmployeeMapper;
import com.wz.project.system.employee.mapper.EmployeeVoMapper;
import com.wz.project.system.prjgroup.domain.Prjgroup;
import com.wz.project.system.prjgroup.entity.PrjgroupEntity;
import com.wz.project.system.prjgroup.service.PrjGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("system/prjgroup")
public class PrjGroupController extends BaseController {

    private String prefix = "system/prjgroup";

    @Autowired
    private PrjGroupService prjGroupService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeVoMapper employeeVoMapper;

    @RequiresPermissions("system:prjgroup:view")
    @GetMapping()
    public ModelAndView prjgroup() {
        return new ModelAndView(prefix + "/prjgroup");
    }


//    @RequiresPermissions("system:prjgroup:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Prjgroup prjgroup) {
        startPage();
        List<Prjgroup> list = prjGroupService.selectPrjGroupList(prjgroup);
        return getDataTable(list);
    }

//    @RequiresPermissions("system:prjgroup:list")
    @RequestMapping("/jpa/list")
    @ResponseBody
    public TableDataInfo jpalist(Prjgroup prjgroup) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<PrjgroupEntity> list = prjGroupService.selectPrjGroupListJpa(prjgroup,pageNum,pageSize);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(list.size());
        return rspData;
    }



    /**
     * 跳转添加员工界面
     * @param prjgroupId 项目组id
     * @param mmap 模型数据
     * @return
     */
    @GetMapping("/addMember/{prjgroupId}/{deptId}")
    public ModelAndView member(@PathVariable Integer prjgroupId,ModelMap mmap,@PathVariable String deptId){
        Prjgroup prjgroup = prjGroupService.selectOneById(prjgroupId);
        Employee employee = new Employee();
        if (!deptId.equals("null")||deptId==null){
            employee.setDeptId(Integer.valueOf(deptId));
        }

//        List<Employee> employees = employeeMapper.selectEmployeeList(employee);
        List<EmployeeVO> employeeVOS1 = prjGroupService.selectEmployeesByPrjgroupId(prjgroup,employee);
//        List<EmployeeVO> employeeVOS = employeeVoMapper.selectEmployeeList(employee);
//        List<EmployeeVO> employeeVOList = employeeVoMapper.selectEmpByPrjId(prjgroupId);

        mmap.put("employees",employeeVOS1);
        mmap.put("prjgroup",prjgroup);
        return new ModelAndView(prefix+"/member");
    }

    /**
     * 跳转添加员工界面
     * @param prjgroupId 项目组id
     * @param mmap 模型数据
     * @return
     */
    @GetMapping("/aaddMember/{prjgroupId}/{deptId}")
    public List<EmployeeVO> amember(@PathVariable Integer prjgroupId, ModelMap mmap, HttpServletRequest request, @PathVariable String deptId){
        Prjgroup prjgroup = prjGroupService.selectOneById(prjgroupId);
        Employee employee = new Employee();
        if (!deptId.equals("null") || deptId != null){
            Integer deptId2 = Integer.valueOf(deptId);
            if (deptId2 == 12 || deptId2 == 1 || deptId2 == 2){
                employee.setDeptId(null);
            }else{
                employee.setDeptId(deptId2);
            }
        }

//        List<Employee> employees = employeeMapper.selectEmployeeList(employee);
        List<EmployeeVO> employeeVOS = employeeVoMapper.selectEmployeeList(employee);

        mmap.put("employees",employeeVOS);
        mmap.put("prjgroup",prjgroup);
        request.setAttribute("a",employeeVOS);

        return employeeVOS;
    }


    @PostMapping("insertMember")
    public Object memberSave(Integer prjgroupId,String employeeIds){
        return toAjax(prjGroupService.saveEmployeesAndPrjgroup(prjgroupId,employeeIds));
    }

    /**
     * 新增角色
     */
    @GetMapping("/add")
    public ModelAndView add() {
        return new ModelAndView(prefix + "/add");
    }

    @PostMapping("/add")
    public Object addSave(Prjgroup prjgroup) {
        return toAjax(prjGroupService.insertOne(prjgroup));
    }

    @GetMapping("edit/{prigroupId}")
    public ModelAndView edit(@PathVariable Integer prigroupId, ModelMap mmap) {
        //查询项目组信息
        Prjgroup prjgroup = prjGroupService.selectOneById(prigroupId);
        mmap.put("prjgroup", prjgroup);
        return new ModelAndView(prefix + "/edit");
    }

    @PostMapping("/edit")
    public Object editSave(Prjgroup prjgroup) {

        return toAjax(prjGroupService.update(prjgroup));

    }

    @PostMapping("remove")
    @RequiresPermissions("system:prjgroup:remove")
    @Log(title = "項目組管理", businessType = BusinessType.DELETE)
    public AjaxResult remove(String ids) {
        return toAjax(prjGroupService.deletByIds(ids));
    }


}
